// 车辆相关的 HTTP 操作
import http from '@/https/http.js'

// 获取车辆列表
export const getVehicleList = async (params = {}) => {
	try {
		// 参数处理，确保必填字段存在默认值
		const requestParams = {
			OpenId: null,
			LicensePlate: null,
			VehicleType: null,
			Status: null,
			PageNum: 1, // 默认第一页
			PageSize: 10, // 默认每页10条
			TotalNum: 0,
			...params
		}

		const res = await http.post('Mini/Vehicle/GetList', requestParams)
		return res
	} catch (error) {
		console.error('获取车辆列表失败:', error)
		throw error
	}
}

// 创建车辆
export const createVehicle = async (vehicleData) => {
	try {
		// 设置默认值
		const requestData = {
			VehicleId: 0,
			LoadCapacity: 0,
			Status: 0, // 默认可用状态
			DriverName: null,
			DriverPhone: null,
			...vehicleData
		}

		const res = await http.post('Mini/Vehicle/Create', requestData)
		return res
	} catch (error) {
		console.error('创建车辆失败:', error)
		throw error
	}
}

// 更新车辆信息
export const updateVehicle = async (vehicleData) => {
	try {
		// 设置默认值
		const requestData = {
			LoadCapacity: 0,
			Status: 0,
			DriverName: null,
			DriverPhone: null,
			...vehicleData
		}

		const res = await http.post('Mini/Vehicle/Update', requestData)
		return res
	} catch (error) {
		console.error('更新车辆信息失败:', error)
		throw error
	}
}

// 根据ID获取车辆详情
export const getVehicleById = async (vehicleId) => {
	try {
		if (!vehicleId) {
			throw new Error('获取车辆详情失败：车辆ID不能为空')
		}
		
		// 使用Mini/Vehicle/GetById接口，按照指定的参数格式
		const requestParams = {
			Id: vehicleId,
			OpenId: null
		}

		const res = await http.post('Mini/Vehicle/GetById', requestParams)
		return res
	} catch (error) {
		console.error('获取车辆详情失败:', error)
		throw error
	}
}

// 删除车辆
export const deleteVehicleById = async (vehicleId) => {
	try {
		if (!vehicleId) {
			throw new Error('删除车辆失败：车辆ID不能为空')
		}
		// 参数处理，确保必填字段存在默认值
		const requestParams = {
			OpenId: null,
			Id: vehicleId
		}

		const res = await http.post('Mini/Vehicle/DeleteById', requestParams)
		return res
	} catch (error) {
		console.error('删除车辆失败:', error)
		throw error
	}
}

// 获取车辆选择列表
// 用于下拉选择的车辆列表
// 根据日期范围和人数筛选可用车辆
export const GetSelectList = async (params = {}) => {
	try {
		// 构建符合Mini_carOpenid格式的参数
		const requestParams = {
			OpenId: params.OpenId || 0, // 小程序请求者的OpenId（用于权限/数据隔离）
			StartDate: params.StartDate || null, // 开始日期（可选，用于筛选可用车辆）
			EndDate: params.EndDate || null, // 结束日期（可选，用于筛选可用车辆）
			count: params.count || null, // 车辆最大人数（不包含司机，可选，用于筛选满足容量要求的车辆）
			...params
		}

		const res = await http.post('Mini/Vehicle/GetSelectList', requestParams)
		return res
	} catch (error) {
		console.error('获取车辆选择列表失败:', error)
		throw error
	}
}

/**
 * 解绑车辆
 * @param {Object} params - 解绑参数
 * @param {number} params.TourGroupId - 旅游团ID
 * @param {string} params.openId - OpenId
 * @returns {Promise} - 返回解绑结果
 */
export const unbindVehicleFromTourGroup = async (params) => {
	try {
  
		const res = await http.post('Mini/VehicleTourGroupBind/UnbindVehicle', params)
		return res
	} catch (error) {
		console.error('解绑车辆失败:', error)
		throw error
	}
}

export default {
	getVehicleList,
	createVehicle,
	updateVehicle,
	getVehicleById,
	deleteVehicleById,
	GetSelectList,
	unbindVehicleFromTourGroup
}